/**
 * Created by gengrong on 16/5/13.
 */
import { createAction, actionTypeCreatorWithNameSpace } from './util';
import agent from 'agent';

const actionTypeCreator = actionTypeCreatorWithNameSpace('runnableGroup');

export const updateUnitMachines = createAction(actionTypeCreator('updateUnitMachines'), (unitId, runtimeId, machineIds) => {
  return agent.post(`/api/runnable-units/${unitId}/machines`)
    .send(machineIds)
    .then(response => response.body);
}, (unitId, runtimeId) => {
  return {
    fetch: {
      processing: true
    },
    unitId,
    runtimeId
  };
});

export const updateUnitSettings = createAction(actionTypeCreator('updateUnitSettings'), (unitId, settings) => {
  return agent.post(`/api/runnable-units/${unitId}/settings`)
    .type('form')
    .send(settings)
    .then(() => settings);
}, (unitId) => {
  return {
    fetch: {
      processing: true
    },
    unitId
  };
});

export const updateUnitBuildConf = createAction(actionTypeCreator('updateUnitBuildConf'), (unitId, runtimeId, conf) => {
  return agent.post(`/api/runnable-units/${unitId}/build-conf`)
    .type('form')
    .send(conf)
    .then(() => conf);
}, (unitId, runtimeId) => {
  return {
    fetch: {
      processing: true
    },
    unitId,
    runtimeId
  };
});

export const updateUnitProperties = createAction(actionTypeCreator('updateUnitProperties'), (unitId, runtimeId, properties) => {
  return agent.post(`/api/runnable-units/${unitId}/properties`)
    .type('form')
    .send(properties)
    .then(() => properties);
}, (unitId, runtimeId) => {
  return {
    fetch: {
      processing: true
    },
    unitId,
    runtimeId
  };
});

export const updateUnitType = createAction(actionTypeCreator('updateUnitType'), (unitId, type) => {
  return agent.post(`/api/runnable-units/${unitId}/type`)
    .type('form')
    .send({ type })
    .then(() => type);
}, (unitId) => {
  return {
    fetch: {
      processing: true
    },
    unitId
  };
});

export const createUnit = createAction(actionTypeCreator('createUnit'), (params) => {
  return agent.post('/api/runnable-units')
    .send(params)
    .then(response => response.body);
}, {
  fetch: {
    processing: true
  }
});

export const deleteUnit = createAction(actionTypeCreator('deleteUnit'), (unitId) => {
  return agent.del(`/api/runnable-units/${unitId}`);
}, {
  fetch: {
    processing: true
  }
});


export const startUnit = createAction(actionTypeCreator('start'), (unitId, image, machineIds) => {
  return agent.post(`/api/runnable-units/${unitId}/start`)
    .type('form')
    .send({ image, machineIds })
    .then(true);
});

export const restartUnit = createAction(actionTypeCreator('restart'), (unitId, image, machineIds) => {
  return agent.post(`/api/runnable-units/${unitId}/restart`)
    .type('form')
    .send({ image, machineIds })
    .then(true);
});

export const stopUnit = createAction(actionTypeCreator('stop'), (unitId, image, machineIds) => {
  return agent.post(`/api/runnable-units/${unitId}/stop`)
    .type('form')
    .send({ image, machineIds })
    .then(true);
});

export const listSuccessBuildsByUnit = createAction(actionTypeCreator('listSuccessBuildsByUnit'), (unitId) => {
  return agent.get(`/api/runnable-units/${unitId}/builds`)
    .then(response => response.body);
}, (unitId) => {
  return {
    fetch: {
      processing: true,
      processingKey: unitId
    },
    unitId
  };
});

export const listMachineStatusByUnitId = createAction(actionTypeCreator('listMachineStatusByUnitId'), (unitId) => {
  return agent.get(`/api/runnable-units/${unitId}/machineStatus`)
    .then(response => response.body);
}, (unitId) => {
  return {
    unitId,
    fetch: {
      processing: true,
      processingKey: unitId,
      cache: {
        key: unitId
      }
    }
  };
});

export const listDeployLogByUnit = createAction(actionTypeCreator('listDeployLogByUnit'), (unitId) => {
  return agent.get(`/api/runnable-units/${unitId}/deployLog`)
    .then(response => response.body);
}, (unitId) => {
  return {
    fetch: {
      processing: true,
      processingKey: unitId
    },
    unitId
  };
});

export const updateDeployLogStatus = createAction(actionTypeCreator('updateDeployLogStatus'));
